﻿using MiniFox.Components;
using System.Security.Claims;

namespace MiniFox.Platform.Applications.Auth
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class AuthorizationClient : Component
    {
        IAuthStateManagement _stateManagement;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stateManagement"></param>
        public AuthorizationClient(IAuthStateManagement stateManagement)
        {
            _stateManagement = stateManagement;
        }
        /// <summary>
        /// 
        /// </summary>
        public IAuthStateManagement StateManagement
        {
            get { return this._stateManagement; }
        }

        /// <summary>
        /// 当前身份信息
        /// </summary>
        public virtual SystemIdentity Identity
        {
            get
            {
                if (this.StateManagement.CurrentIdentity == null)
                {
                    this.StateManagement.CurrentIdentity = this.GetIdentity();
                }
                return this.StateManagement.CurrentIdentity;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected abstract SystemIdentity GetIdentity();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        protected internal abstract SystemIdentity GetIdentity(string token);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <returns></returns>
        public abstract string[] GetRoles(string requestUrl);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <param name="entryUrl"></param>
        /// <returns></returns>
        public abstract string[] GetRoles(string requestUrl, string entryUrl);
        /// <summary>
        /// 
        /// </summary>
        public virtual void ClearState()
        {
            this.StateManagement.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected abstract ClaimsPrincipal CreatePrincipal();
        /// <summary>
        /// 
        /// </summary>
        public virtual ClaimsPrincipal Principal
        {
            get
            {
                if (this.StateManagement.CurrentPrincipal == null)
                {
                    this.StateManagement.CurrentPrincipal = this.CreatePrincipal();
                }
                return this.StateManagement.CurrentPrincipal;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public abstract bool IsExceptionUrl(string url);
        /// <summary>
        /// 
        /// </summary>
        public abstract bool VerifyTokenState();
    }
}
